//
//  ViewController.m
//  TripHelp
//
//  Created by lanou3g on 16/8/13.
//  Copyright © 2016年 110. All rights reserved.
//

#import "ViewController.h"
//科大讯飞语音识别功能更回调方法的接口文件
#import "iflyMSC/IFlyRecognizerViewDelegate.h"
//科大讯飞语音识别功能声音识别视图
#import "iflyMSC/IFlyRecognizerView.h"
//科大讯飞语音识别功能自定义常量
#import <iflyMSC/IFlySpeechConstant.h>

@interface ViewController ()<IFlyRecognizerViewDelegate>


//接收相关结果数组
@property(nonatomic,strong)NSMutableString *resultString;

@property(nonatomic,strong)UITextField *serach;

//语音识别视图
@property(nonatomic,strong)IFlyRecognizerView *iFlyRecognizerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithRed:0.446 green:0.996 blue:1.000 alpha:1.000];
    
    UITextField *search = [[UITextField alloc]initWithFrame:CGRectMake(30, 64, [UIScreen mainScreen].bounds.size.width * 4 / 5, 40)];
    search.placeholder = @"请输入搜索内容";
    search.textAlignment = NSTextAlignmentCenter;
    search.backgroundColor = [UIColor colorWithRed:0.976 green:0.358 blue:1.000 alpha:1.000];
    [self.view addSubview:search];
    self.serach = search;
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake([UIScreen mainScreen].bounds.size.width -510, 64, 40, 40);
    [btn1 setTitle:@"搜索" forState:UIControlStateNormal];
    [btn1 setTag:1001];
    btn1.backgroundColor = [UIColor brownColor];
    [btn1 addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    //初始化声音识别视图对象
    self.iFlyRecognizerView = [[IFlyRecognizerView alloc]initWithCenter:self.view.center];
    self.iFlyRecognizerView.delegate = self;
    
    
}
-(void)btn{
    [self listening];
}

-(void)listening{
    
    //设置语音识别结果应用为普通文本
    [self.iFlyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    //设置前端检测时间为5000ms
    [self.iFlyRecognizerView setParameter:@"5000" forKey:[IFlySpeechConstant VAD_BOS]];
    
    //设置后端点检测时间为700ms
    [self.iFlyRecognizerView setParameter:@"700" forKey:[IFlySpeechConstant VAD_EOS]];
    
    //采样率为8000
    [self.iFlyRecognizerView setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];
    
    //设置返回结果包含的标点符号
    [self.iFlyRecognizerView setParameter:@"0" forKey:[IFlySpeechConstant ASR_PTT]];
    
    //设置语音识别后数据的返回数据结果类型
    [self.iFlyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];
    
    //    设置在docment文件夹下的缓存文件名“temp.asr”
    [self.iFlyRecognizerView setParameter:@"temp.asr" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    
    //设置自定义参数
    [self.iFlyRecognizerView setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
    
    //    开始识别语音
    [self.iFlyRecognizerView start];
    NSLog(@"识别完毕");

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//识别成功后
-(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
    //初始化结果字符串
    self.resultString = [NSMutableString string];
    NSDictionary *dic = [resultArray objectAtIndex:0];
    
    for (NSMutableString *str in dic) {
        [self.resultString appendFormat:@"%@",str];
        
    }
    //自定义控件的显示
    self.serach.text = [NSString stringWithFormat:@"%@,%@",self.serach.text,self.resultString];
    // self.showTextView.text = [self.showTextView.text stringByAppendingString:self.resultString];
    
}

-(void)onError:(IFlySpeechError *)error{
    NSLog(@"失败");
}

@end
